<?php

namespace Muyuxuanpay\threePay\src\hk\merchant;

use Muyuxuanpay\threePay\BaseTrait;
use Muyuxuanpay\threePay\MerchantIncoming;
use Muyuxuanpay\tool\Image;
use Muyuxuanpay\tool\Mtool;

class Incoming implements MerchantIncoming
{
    use BaseTrait;
    use MerchantTrait;
    protected $domain = "http://47.95.131.62:8080/api/";
    protected $urls = [
        'upImage'=>'v1/merchant-image/upload',
        'upFile'=>'v1/merchant-image/video-upload',
        'applyFor'=>'v2/merchant/aggregation-apply',
        'incomingStatusQuery'=>'v1/sub/query-merchant-info',
        'wxAuth'=>'v1/wx-auth/apply',
        'alipayAuth'=>'v1/ali-auth/create',
        'bankAuth'=>'v1/ali-auth/create',
        'applyConfirm'=>'v1/merchant-terminal/new-bind',
        'TerminalUnbinding'=>'v1/merchant-terminal/new-unbound',
        'TerminalBindingQuery'=>'v1/merchant-terminal/new-query',
    ];
    /**
     * 图片上传
     * @param array $data
     * @return mixed
     */
    public function upImage(string $imagePath,string $order_no=''):string|int
    {
        if(empty($imagePath)){
            throw new \Exception('图片路径不能为空');
        }
        $str = Image::path2Base64($imagePath);
        $data = [
            'image' => $str,
            'accessid' => $this->config['accessid']
        ];
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        $back_sign = $this->getSign($res);
        if($back_sign != $res['sign']){
            throw new \Exception('签名错误');
        }
        if($res['return_code'] != "10000"){
            throw new \Exception($res['return_msg']);
        }
        $this->resalut = $res;
        return $res['image_id'];
    }
    /**
     * 文件上传（包括视频）
     * @param array $filePath
     * @return mixed
     */
    public function upFile(string $filePath):string|int{
        if(empty($imagePath)){
            throw new \Exception('图片路径不能为空');
        }
        $str = Image::path2Base64($filePath);
        $data = [
            'video' => $str,
            'accessid' => $this->config['accessid']
        ];
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        $back_sign = $this->getSign($res);
        if($back_sign != $res['sign']){
            throw new \Exception('签名错误');
        }
        if($res['return_code'] != "10000"){
            throw new \Exception($res['return_msg']);
        }
        $this->resalut = $res;
        return $res['videoId'];
    }

    /**
     * 提交进件
     * @param string|int $order_no 订单号
     * @param array $merchant_data
     * @param array $ban_data
     * @param array $image_data
     * @param array $product_data
     * @param array $settle_data
     * @param string $notify_url
     * @param array $event
     * @return array
     */
    public function applyFor(string|int $order_no='',array $merchant_data = [],array $ban_data=[],array $image_data=[],array $product_data=[],array $settle_data=[],string $notify_url='',array $event=[]):array{
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'accessid'=>$this->config['accessid'],
            'agent_apply_no'=>empty($order_no)?Mtool::getOrderNum():$order_no,
//            'merch_no'=>empty($event['merch_no'])?Mtool::getOrderNum(10):$event['merch_no'],
            'merchant_data'=>$merchant_data,
            'bankcard_data'=>$ban_data,
            'image_data'=>$image_data,
            'product_data'=>$product_data,
            'settle_data'=>$settle_data,
            'notify_url'=>$notify_url,
            'agent_rate_formwork_id'=>empty($event['agent_rate_formwork_id'])?"":$event['agent_rate_formwork_id']
        ];
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        if($res['return_code']!="10000"){
            throw new \Exception($res['return_msg']);
        }
        $this->resalut = $res;
        return $res;
    }

    /**
     * 确认进件
     * @param string|int $merch_no 商户号
     * @param string $signm 标识
     * @param string $terminal_address 终端地址
     * @param array $event 其他
     * @return mixed
     */
    public function applyConfirm(string|int $merch_no,string $signm,string $terminal_address,array $event=[]){
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'accessid'=>$this->config['accessid'],
        ];
        if(!empty($event['storeId'])){
            $data['storeId'] = $event['storeId'];
        }
        $data['merch_no'] = $merch_no;
        if(!empty($event['agent_apply_no'])){
            $data['agent_apply_no'] = $event['agent_apply_no'];
        }
        if(empty($signm)){
            throw new \Exception('signm不能为空');
        }
        $data['sn'] = $signm;
        if(!empty($event['code'])){
            $data['code'] = $event['code'];
        }
        if(empty($event['terminal_address'])){
            throw new \Exception('terminal_address不能为空');
        }
        $data['terminal_address'] = $terminal_address;
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        $this->resalut = $res;
        if($res['return_code']!="10000"){
            throw new \Exception($res['return_msg']);
        }
        return $res['pn'];
    }
    public function TerminalUnbinding(string|int $agent_apply_no,string $sn='',string $pn='',array $event=[])
    {
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'accessid'=>$this->config['accessid'],
        ];
        if(empty($agent_apply_no)){
            throw new \Exception('agent_apply_no不能为空');
        }
        $data['agent_apply_no'] = $agent_apply_no;
        if(!empty($sn)){
            $data['sn'] = $sn;
        }
        if(!empty($pn)){
            $data['pn'] = $pn;
        }
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        $this->resalut = $res;
        if($res['return_code']!="10000"){
            throw new \Exception($res['return_msg']);
        }
        return $res['pn'];
    }
    public function TerminalBindingQuery(string|int $agent_apply_no,string $sn='',string $pn='',array $event=[])
    {
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'accessid'=>$this->config['accessid'],
        ];
        if(empty($agent_apply_no)){
            throw new \Exception('agent_apply_no不能为空');
        }
        $data['agent_apply_no'] = $agent_apply_no;
        if(!empty($sn)){
            $data['sn'] = $sn;
        }
        if(!empty($pn)){
            $data['pn'] = $pn;
        }
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        $this->resalut = $res;
        if($res['return_code']!="10000"){
            throw new \Exception($res['return_msg']);
        }
        return $res['report_result'];
    }

    /**
     * 修改资料
     * @param string $merch_no 商户号
     * @param array $type 请求类型
     * @param array $event 其他
     * @return mixed
     */
    public function ModifyData(string $merch_no,$type,array $event=[]){
        switch ($type){
            case "change_status":
                return $this->change_status($merch_no,$event);
                break;
            case "change_merchant_info":
                return $this->change_merchant_info($merch_no,$event);
                break;
            default:
                throw new \Exception('type类型错误');
        }
    }
    protected function change_status(string $merch_no,array $event=[])
    {
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'accessid'=>$this->config['accessid'],
        ];
        if(!empty($event['merchant_data'])){
            $data['merchant_data'] = $event['merchant_data'];
        }
        if(!empty($event['image_data'])){
            $data['image_data'] = $event['image_data'];
        }
    }

    /**
     * 获取进件状态异步通知
     * @param array $data
     * @return mixed
     */
    public function incomingStatusNotify(array $data){

    }

    /**
     * 查询进件状态
     * @param string|int $order_no
     * @param array $event
     * @return int //status=1成功,2失败,3审核中,4已撤销
     */
    public function incomingStatusQuery(string|int $order_no,array $event = []):int{
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'accessid'=>$this->config['accessid'],
        ];
        if(!empty($order_no)){
            $data['merch_no'] = $order_no;
        }
        if(!empty($event['agent_apply_no'])){
            $data['agent_apply_no'] = $event['agent_apply_no'];
        }
        $data['sign'] = $this->getSign($data);
        $url = $this->domain.$this->urls[__FUNCTION__];
        $res = $this->post($url,$data);
        $this->resalut = $res;
        $status = match (intval($res['status'])){
            7=>1,
            1=>2,
            4=>2,
            5=>2,
            6=>2,
            8=>4,
            9=>4,
            default=>3,
        };
        //status=1成功,2失败,3审核中,4已撤销
        return $status;
    }

    /**
     * 查询额度
     * @param array $data
     * @return mixed
     */
    public function queryQuota(array $data){

    }

    /**
     * 结算异步通知
     * @return mixed
     */
    public function settlementNotify(array $data){

    }

    /**
     * 结算查询
     * @return mixed
     */
    public function settlementQuery(array $data){

    }


    /**
     * 提交认证
     * @param string|int $wx_merch_no 微信商户编号
     * @param array $business_type 提交类型
     * @param array $contact_info 联系人信息
     * @param array $subject_info 主体信息
     * @param array $identification_info 法人身份信息
     * @param array $event 其他信息
     * @return void
     */
    public function authSubmit(string|int $wx_merch_no,string $business_type='all',array $contact_info=[],array $subject_info=[],array $identification_info=[],array $event=[]){
        if($business_type=='wx'){
            return $this->wxAuth($wx_merch_no,$business_type,$contact_info,$subject_info,$identification_info,$event);
        }elseif ($business_type=='alipay'){
            return $this->alipayAuth($wx_merch_no,$business_type,$contact_info,$subject_info,$identification_info,$event);
        }elseif ($business_type=='bank'){
            return $this->bankAuth($wx_merch_no,$business_type,$contact_info,$subject_info,$identification_info,$event);
        }
    }
    protected function bankAuth(string|int $wx_merch_no,string $business_type='all',array $contact_info=[],array $subject_info=[],array $identification_info=[],array $event=[])
    {

    }
    protected function alipayAuth(string|int $wx_merch_no,string $business_type='all',array $contact_info=[],array $subject_info=[],array $identification_info=[],array $event=[])
    {

    }
    protected function wxAuth(string|int $wx_merch_no,string $business_type='all',array $contact_info=[],array $subject_info=[],array $identification_info=[],array $event=[])
    {
        if(empty($event['merch_no'])){
            throw new \Exception('商户号不能为空');
        }
        if(empty($wx_merch_no)){
            throw new \Exception('微信商户号不能为空');
        }
        $data = [
            'agent_no'=>$this->config['agent_no'],
            'merch_no'=>$event['merch_no'],
            'wx_merch_no'=>$wx_merch_no,
            'business_code'=>''
        ];
    }

    /**
     * 认证状态查询
     * @param array $data
     * @return mixed
     */
    public function authStatusQuery(array $data){

    }

    /**
     * 确认认证
     * @param array $data
     * @return mixed
     */
    public function authConfrim(array $data){

    }
    /**
     * 撤销认证
     * @param array $data
     * @return mixed
     */
    public function revokeAuth(array $data){

    }
}